/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pt.iasd.youthshelter.pathfinder.wizards;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import java.util.List;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.plaf.basic.BasicComboBoxRenderer;
import org.jdesktop.swingx.JXDatePicker;
import org.openide.util.Lookup;
import pt.iasd.youthshelter.datamodel.Pathfinder;
import pt.iasd.youthshelter.datamodel.Role;
import pt.iasd.youthshelter.datamodel.service.PathfinderController;
import pt.iasd.youthshelter.datamodel.service.RoleController;

public final class PathfinderRoleVisualPanel extends JPanel implements ActionListener{
    private static final ResourceBundle bundle = java.util.ResourceBundle.getBundle("pt/iasd/youthshelter/pathfinder/Bundle");
    
    /* Logging object. */
    private Logger logger = Logger.getLogger(this.getClass().getName());

    /**
     * Creates new form PathfinderRoleVisualPanel
     */
    public PathfinderRoleVisualPanel() {
        this(null, null, null, null, null);
    }
    
    public PathfinderRoleVisualPanel(Pathfinder pathfinder, Role role, String place, Date start, Date end) {
        initComponents();
        boolean editionMode = false;
        if(pathfinder != null) {
            this.PathfinderComboBox.setSelectedItem(pathfinder);
            editionMode = true;
        }
        if(role != null) {
            this.RoleComboBox.setSelectedItem(role);
            editionMode = true;
        }
        if(place != null && !place.isEmpty()) {
            this.PlaceTextField.setText(place);
            editionMode = true;
        }
        if(start != null) {
            this.StartDatePicker.setDate(start);
            editionMode = true;
        }
        if(end != null) {
            this.EndDatePicker.setDate(end);
            editionMode = true;
        }
        if(editionMode) {
            actionPerformed(null);
        }
    }

    @Override
    public String getName() {
        return WizardKeys.PROP_PATHFINDER_ROLE_STEPS[0];
    }

    public JXDatePicker getEndDatePicker() {
        return EndDatePicker;
    }

    public JComboBox getPathfinderComboBox() {
        return PathfinderComboBox;
    }

    public JTextField getPlaceTextField() {
        return PlaceTextField;
    }

    public JComboBox getRoleComboBox() {
        return RoleComboBox;
    }

    public JXDatePicker getStartDatePicker() {
        return StartDatePicker;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        PathfinderLabel = new javax.swing.JLabel();
        PathfinderComboBox = new javax.swing.JComboBox();
        RoleLabel = new javax.swing.JLabel();
        RoleComboBox = new javax.swing.JComboBox();
        PlaceLabel = new javax.swing.JLabel();
        PlaceTextField = new javax.swing.JTextField();
        RoleSeparator = new javax.swing.JSeparator();
        StartDateLabel = new javax.swing.JLabel();
        StartDatePicker = new org.jdesktop.swingx.JXDatePicker();
        EndDateLabel = new javax.swing.JLabel();
        EndDatePicker = new org.jdesktop.swingx.JXDatePicker();

        org.openide.awt.Mnemonics.setLocalizedText(PathfinderLabel, bundle.getString("PathfinderRoleVisualPanel.PathfinderLabel.text"));

        List<Pathfinder> pathfinders = loadPathfinderComboBoxData();
        PathfinderComboBox.setModel((pathfinders == null || pathfinders.isEmpty())? new DefaultComboBoxModel() : new DefaultComboBoxModel(pathfinders.toArray(new Pathfinder[pathfinders.size()])));
        if(pathfinders != null && !pathfinders.isEmpty()) {
            PathfinderComboBox.setSelectedIndex(0);
        }
        PathfinderComboBox.setPreferredSize(new java.awt.Dimension(300, 20));
        PathfinderComboBox.setRenderer(new PathfinderRenderer());
        PathfinderComboBox.addActionListener(this);

        org.openide.awt.Mnemonics.setLocalizedText(RoleLabel, bundle.getString("PathfinderRoleVisualPanel.RoleLabel.text"));

        List<Role> roles = loadRoleComboBoxData();
        RoleComboBox.setModel((roles == null || roles.isEmpty())? new DefaultComboBoxModel() : new DefaultComboBoxModel(roles.toArray(new Role[roles.size()])));
        if(roles != null || !roles.isEmpty()) {
            RoleComboBox.setSelectedIndex(0);
        }
        RoleComboBox.setPreferredSize(new java.awt.Dimension(300, 20));
        RoleComboBox.setRenderer(new RoleRenderer());
        RoleComboBox.addActionListener(this);

        org.openide.awt.Mnemonics.setLocalizedText(PlaceLabel, bundle.getString("PathfinderRoleVisualPanel.PlaceLabel.text"));

        PlaceTextField.setPreferredSize(new java.awt.Dimension(300, 20));
        PlaceTextField.addActionListener(this);

        org.openide.awt.Mnemonics.setLocalizedText(StartDateLabel, bundle.getString("PathfinderRoleVisualPanel.StartDateLabel.text"));

        StartDatePicker.setDate(new Date());
        StartDatePicker.setMaximumSize(new java.awt.Dimension(150, 22));
        StartDatePicker.setMinimumSize(new java.awt.Dimension(150, 22));
        StartDatePicker.setPreferredSize(new java.awt.Dimension(150, 22));
        StartDatePicker.addActionListener(this);

        org.openide.awt.Mnemonics.setLocalizedText(EndDateLabel, bundle.getString("PathfinderRoleVisualPanel.EndDateLabel.text"));

        EndDatePicker.setDate(new Date());
        EndDatePicker.setMaximumSize(new java.awt.Dimension(150, 22));
        EndDatePicker.setMinimumSize(new java.awt.Dimension(150, 22));
        EndDatePicker.setPreferredSize(new java.awt.Dimension(150, 22));
        EndDatePicker.addActionListener(this);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(RoleSeparator, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(PlaceLabel)
                                    .addComponent(RoleLabel)
                                    .addComponent(PathfinderLabel))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(PathfinderComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(RoleComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(PlaceTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(EndDateLabel)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(EndDatePicker, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(StartDateLabel)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(StartDatePicker, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(PathfinderLabel)
                    .addComponent(PathfinderComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(RoleLabel)
                    .addComponent(RoleComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(PlaceLabel)
                    .addComponent(PlaceTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(RoleSeparator, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(StartDateLabel)
                    .addComponent(StartDatePicker, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(EndDateLabel)
                    .addComponent(EndDatePicker, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel EndDateLabel;
    private org.jdesktop.swingx.JXDatePicker EndDatePicker;
    private javax.swing.JComboBox PathfinderComboBox;
    private javax.swing.JLabel PathfinderLabel;
    private javax.swing.JLabel PlaceLabel;
    private javax.swing.JTextField PlaceTextField;
    private javax.swing.JComboBox RoleComboBox;
    private javax.swing.JLabel RoleLabel;
    private javax.swing.JSeparator RoleSeparator;
    private javax.swing.JLabel StartDateLabel;
    private org.jdesktop.swingx.JXDatePicker StartDatePicker;
    // End of variables declaration//GEN-END:variables

    @Override
    public void actionPerformed(ActionEvent e) {
        firePropertyChange(WizardKeys.PROP_ANY, 0, 1);
    }

    // <editor-fold defaultstate="collapsed" desc="Inner Classes">
    private class PathfinderRenderer extends BasicComboBoxRenderer {
        @Override
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            if(value instanceof Pathfinder) {
                setText(((Pathfinder)value).getName());
            } else {
                setText("");
            }
            return this;
        }
    }
    
    private class RoleRenderer extends BasicComboBoxRenderer {
        @Override
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            if(value instanceof Role) {
                setText(((Role)value).getName());
            } else {
                setText("");
            }
            return this;
        }
    } // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="ComboBox Data Loading Code">
    private List loadPathfinderComboBoxData() {
        PathfinderController controller = Lookup.getDefault().lookup(PathfinderController.class);
        if(controller != null) {
            try{
                List list = controller.findActivePathfinderEntities();
                if(!list.isEmpty()) {
                    return list;
                }
            } catch(Exception ex) {
                logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
            }
        }
        return null;
    }
    
    private List loadRoleComboBoxData() {
        RoleController controller = Lookup.getDefault().lookup(RoleController.class);
        if(controller != null) {
            try{
                List list = controller.findRoleEntities();
                if(!list.isEmpty()) {
                    return list;
                }
            } catch(Exception ex) {
                logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
            }
        }
        return null;
    }// </editor-fold>
}
